package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceFilterEntity;
import com.sgb.kjwl.view.ui.adapter.transport.CarLengthAdapter3;
import com.sgb.kjwl.view.ui.adapter.transport.CarTypeAdapter3;
import com.sgb.kjwl.view.ui.adapter.transport.LoadGoodsTimeAdapter3;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class GoodsMorePopWindow3 extends BasePopWindow {

    private List<GoodsInfoTypeEntity3> carsTypeList;
    private List<GoodsInfoTypeEntity3> carsLengthList;
    private CarTypeAdapter3 carTypeAdapter;
    private CarLengthAdapter3 lengthAdapter;
    private LoadGoodsTimeAdapter3 timeAdapter;
    private List<GoodsInfoTypeEntity3> currentTypeList = new ArrayList<>();
    private List<GoodsInfoTypeEntity3> lengthtempList = new ArrayList<>();
    private List<GoodsInfoTypeEntity3> selectTypeList = new ArrayList<>();
    private List<GoodsInfoTypeEntity3> selectLengthList = new ArrayList<>();
    private boolean hide;

    public GoodsMorePopWindow3(Context context) {
        super(context,DIR_RIGHT);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN|
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_more3;
    }

    @Override
    protected void initView(PopViewHolder holder) {

        GoodsSourceFilterEntity entity = new GoodsSourceFilterEntity();

        RelativeLayout root_layout = holder.getPop().findViewById(R.id.root_layout);
        EditText et_wight_min = holder.getPop().findViewById(R.id.goods_weight_min);
        EditText et_wight_max = holder.getPop().findViewById(R.id.goods_weight_max);
        MaxRecyclerView loadTimeList = holder.getPop().findViewById(R.id.goods_load_time_list);
        MaxRecyclerView carTypeList = holder.getPop().findViewById(R.id.goods_car_type_list);
        MaxRecyclerView carLengthList = holder.getPop().findViewById(R.id.goods_car_length_list);
        TextView tv_reset = holder.getPop().findViewById(R.id.reset_step);
        TextView tv_sure = holder.getPop().findViewById(R.id.sure_step);

        InputMethodManager im = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        setTouchInterceptor((view, motionEvent) -> {
            if(null!=im){
                im.hideSoftInputFromWindow(et_wight_max.getWindowToken(),0);
            }
            return false;
        });

        //重置
        tv_reset.setOnClickListener(view -> {
            et_wight_min.setText("");
            et_wight_max.setText("");
            carTypeAdapter.resetCheckDatas();
            carTypeAdapter.setDefItem(-1);
            lengthAdapter.resetCheckDatas();
            lengthAdapter.setDefItem(-1);
            timeAdapter.resetCheckDatas();
            timeAdapter.setDefItem(-1);

            entity.setMaxWeight("");
            entity.setMinWeight("");
            entity.setLengthType(null);
            entity.setCarType(null);
            entity.setTimeType(null);

        });
        //确定
        tv_sure.setOnClickListener(view -> {
            entity.setMinWeight(et_wight_min.getText().toString());
            entity.setMaxWeight(et_wight_max.getText().toString());
            onResultCallBack.result(entity);
            dissMiss();
        });

        List<GoodsInfoTypeEntity3> loadGoodsTime = new ArrayList<>();
        GoodsInfoTypeEntity3 t = new GoodsInfoTypeEntity3();
        t.setName("明天");
        loadGoodsTime.add(t);
        GoodsInfoTypeEntity3 t1 = new GoodsInfoTypeEntity3();
        t1.setName("今天");
        loadGoodsTime.add(t1);
        GoodsInfoTypeEntity3 t2 = new GoodsInfoTypeEntity3();
        t2.setName("后天");
        loadGoodsTime.add(t2);
        GoodsInfoTypeEntity3 t4 = new GoodsInfoTypeEntity3();
        t4.setName("不限");
        loadGoodsTime.add(t4);

        //装货时间
        timeAdapter = new LoadGoodsTimeAdapter3(mContext,loadGoodsTime);
        loadTimeList.setLayoutManager(new GridLayoutManager(mContext,3));
        loadTimeList.setAdapter(timeAdapter);
        timeAdapter.setItemSelect(true);
        timeAdapter.setDefItem(-1);
        timeAdapter.setOnItemClick(new AdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 s, int position) {
                entity.setTimeType(s);
            }
        });

        //车型
        carTypeAdapter = new CarTypeAdapter3(mContext,null);
        carTypeList.setLayoutManager(new GridLayoutManager(mContext,3));
        carTypeList.setAdapter(carTypeAdapter);
        carTypeAdapter.setMultiSelect(true);
        carTypeAdapter.setDefItem(-1);
        carTypeAdapter.setOnItemMultiClick((goodsInfoTypeEntity3s, position) -> {
//            Log.v("filter_length",goodsInfoTypeEntity3s.get(0).getName());
            entity.setCarType(goodsInfoTypeEntity3s);
        });

        ImageView iv_carType_more = holder.getPop().findViewById(R.id.car_type_more);
        iv_carType_more.setOnClickListener(view -> {
            if(null!=carTypeAdapter.getDatas() && carTypeAdapter.getDatas().size()>0){
                if(carTypeAdapter.getDatas().size() < carsTypeList.size()){
                    carTypeAdapter.setDatas(carsTypeList);
                    iv_carType_more.setImageResource(R.mipmap.btn_expand);
                }else{
                    carTypeAdapter.setDatas(currentTypeList);
                    iv_carType_more.setImageResource(R.mipmap.btn_close);
                }
            }
        });

        //车长
        lengthAdapter = new CarLengthAdapter3(mContext,null);
        carLengthList.setLayoutManager(new GridLayoutManager(mContext,3));
        carLengthList.setAdapter(lengthAdapter);
        lengthAdapter.setMultiSelect(true);
        lengthAdapter.setDefItem(-1);
        lengthAdapter.setOnItemMultiClick((goodsInfoTypeEntity3s, position) -> {
//            Log.v("filter_length",goodsInfoTypeEntity3s.get(0).getName());
            entity.setLengthType(goodsInfoTypeEntity3s);
        });

        ImageView iv_carLenght_more = holder.getPop().findViewById(R.id.car_lenght_more);
        iv_carLenght_more.setOnClickListener(view -> {

            if(null!=lengthAdapter.getDatas() && lengthAdapter.getDatas().size()>0){
                if(lengthAdapter.getDatas().size() < carsLengthList.size()){
                    lengthAdapter.setDatas(carsLengthList);
                    iv_carLenght_more.setImageResource(R.mipmap.btn_expand);
                }else{
                    lengthAdapter.setDatas(lengthtempList);
                    iv_carLenght_more.setImageResource(R.mipmap.btn_close);
                }
            }
        });

    }

    public void setData(List<GoodsInfoTypeEntity3> list,List<GoodsInfoTypeEntity3> lengthlist){
        this.carsTypeList = list;
        this.carsLengthList = lengthlist;

        if(null!=carsTypeList){
            if(carsTypeList.size()> 6){
                currentTypeList = carsTypeList.subList(0,6);
            }else{
                currentTypeList = carsTypeList;
            }
            carTypeAdapter.setDatas(currentTypeList);
        }

        if(null!=carsLengthList){
            if(carsLengthList.size()> 6){
                lengthtempList = carsLengthList.subList(0,6);
            }else{
                lengthtempList = carsLengthList;
            }
            lengthAdapter.setDatas(lengthtempList);
        }
    }

    public void hideTypeAndLength(){
        getContentView().findViewById(R.id.layout_car_type).setVisibility(View.GONE);
        getContentView().findViewById(R.id.layout_car_length).setVisibility(View.GONE);
    }
}
